package com.ruoyi.web.controller.system;

import java.io.FileNotFoundException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.service.IStudentService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-11
 */
@Controller
@RequestMapping("/system/student")
public class StudentController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    private String prefix = "system/student";
	
	@Autowired
	private IStudentService studentService;
	
	@RequiresPermissions("system:student:view")
	@GetMapping()
	public String student()
	{
	    return prefix + "/student";
	}
	
	/**
	 * 查询学生列表
	 */
	@RequiresPermissions("system:student:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Student student)
	{
		startPage();
        List<Student> list = studentService.selectStudentList(student);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出学生列表
	 */
	@RequiresPermissions("system:student:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Student student)
    {
    	List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.exportExcel(list, "student");
    }
	/**
	 * 导入学生列表
	 */
	@Log(title = "学生管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:student:importExcel")
	@PostMapping("/importExcel")
	@ResponseBody
	public AjaxResult importExcel(@RequestParam("file") MultipartFile file)
	{
		if (file.isEmpty())
		{
			return AjaxResult.error("文件不能为空");
		}
		try {
			ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
			List<Student> teacherList=util.importExcel(file.getInputStream());
			if (teacherList.size()>0){
				for (int i=1;i<teacherList.size();i++){
					Student student =new Student();
					student.setStuName(teacherList.get(i).getStuName());
					student.setStuNum(teacherList.get(i).getStuNum());
					studentService.insertStudent(student);
				}

			}
		} catch (FileNotFoundException e) {
			log.error("导入异常",e);
		} catch (Exception e) {
			log.error("保存数据",e);
		}
		return AjaxResult.success("导入成功");
	}
	/**
	 * 新增学生
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存学生
	 */
	@RequiresPermissions("system:student:add")
	@Log(title = "学生", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Student student)
	{		
		return toAjax(studentService.insertStudent(student));
	}

	/**
	 * 修改学生
	 */
	@GetMapping("/edit/{stuId}")
	public String edit(@PathVariable("stuId") Integer stuId, ModelMap mmap)
	{
		Student student = studentService.selectStudentById(stuId);
		mmap.put("student", student);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存学生
	 */
	@RequiresPermissions("system:student:edit")
	@Log(title = "学生", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Student student)
	{		
		return toAjax(studentService.updateStudent(student));
	}
	
	/**
	 * 删除学生
	 */
	@RequiresPermissions("system:student:remove")
	@Log(title = "学生", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(studentService.deleteStudentByIds(ids));
	}
	
}
